package cn.iocoder.yudao.module.infra.controller.admin.ocr;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infra.controller.admin.ocr.vo.record.OcrRecordPageReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.ocr.vo.record.OcrRecordRespVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.ocr.OcrRecordDO;
import cn.iocoder.yudao.module.infra.service.ocr.OcrRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - OCR记录")
@RestController
@RequestMapping("/infra/ocr-record")
@Validated
public class OcrRecordController {

    @Resource
    private OcrRecordService ocrRecordService;

    @DeleteMapping("/delete")
    @Operation(summary = "删除OCR记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('infra:ocr-record:delete')")
    public CommonResult<Boolean> deleteOcrRecord(@RequestParam("id") Long id) {
        ocrRecordService.deleteOcrRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Operation(summary = "批量删除OCR记录")
    @Parameter(name = "ids", description = "编号列表", required = true)
    @PreAuthorize("@ss.hasPermission('infra:ocr-record:delete')")
    public CommonResult<Boolean> deleteOcrRecordList(@RequestParam("ids") List<Long> ids) {
        ocrRecordService.deleteOcrRecordList(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得OCR记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('infra:ocr-record:query')")
    public CommonResult<OcrRecordRespVO> getOcrRecord(@RequestParam("id") Long id) {
        OcrRecordDO record = ocrRecordService.getOcrRecord(id);
        return success(BeanUtils.toBean(record, OcrRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得OCR记录分页")
    @PreAuthorize("@ss.hasPermission('infra:ocr-record:query')")
    public CommonResult<PageResult<OcrRecordRespVO>> getOcrRecordPage(@Valid OcrRecordPageReqVO pageVO) {
        PageResult<OcrRecordDO> pageResult = ocrRecordService.getOcrRecordPage(pageVO);
        return success(BeanUtils.toBean(pageResult, OcrRecordRespVO.class));
    }

    @GetMapping("/get-by-file")
    @Operation(summary = "根据文件ID获得OCR记录")
    @Parameter(name = "fileId", description = "文件ID", required = true)
    @PreAuthorize("@ss.hasPermission('infra:ocr-record:query')")
    public CommonResult<OcrRecordRespVO> getOcrRecordByFileId(@RequestParam("fileId") Long fileId) {
        OcrRecordDO record = ocrRecordService.getOcrRecordByFileId(fileId);
        return success(BeanUtils.toBean(record, OcrRecordRespVO.class));
    }
}